﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace CH_k
{
    [Serializable]
    [DefaultPropertyAttribute("Свойства")]
    public class Element
    {
        [CategoryAttribute("Свойства"), DescriptionAttribute("Тип элемента")]
        public string Тип
        {
            get
            {
                return ElType;
            }
        }
        static public List<Element> seList = new List<Element>();
        static public Element newElement = null;
        public List<Element> inElList;
        public List<Element> outElList;
        static public Element sSelEl = null;

        static public void TryAdd()
        {
            if (newElement.ElType != "ELine")
            {
                seList.Add(newElement);
                newElement = null;
            }
        }

        public string ElType;

        [NonSerialized()]
        public Pen cPen;
        public int input_n;
        public Point dec;
        public Point size;

        public bool oldState;
        public bool alreadyRun;
        public bool invers;
        public bool selected;

        public Point input_p;
        public Point output_p;

        public virtual bool isPosChage(Point _p)
        {
            return !(_p == dec);
        }
        public virtual void pCalc() { }
        public virtual void ELineUpdate() { }
        public virtual void Draw(Graphics _g) { }
        public virtual void PosUpdate(Point _p)
        {
            dec = _p;
        }
        public virtual void clearRun()
        {
            alreadyRun = false;
        }
        public Element()
        {
            cPen = Pens.Black;
            ElType = "undefined";
            inElList = new List<Element>();
            outElList = new List<Element>();
        }
        public virtual bool hover(Point MouseCord)
        {
            return ((MouseCord.X > dec.X + 10 && MouseCord.X < dec.X + 10 + size.X) && (MouseCord.Y > dec.Y && MouseCord.Y < dec.Y + size.Y));
        }
        public virtual bool ClickLeft(Point MouseCord)
        {
            if (hover(MouseCord))
            {
                selected = true;
                cPen = new Pen(Color.Red, 3);
                return true;
            }
            else
            {
                selected = false;
                cPen = Pens.Black;
                return false;
            }
        }

        public virtual void Delete()
        { }
        public virtual bool TrySetInput(Point _p, Element _e)
        {
            return false;
        }
        public virtual bool TrySetOutput(Point _p, int i, Element _e)
        {
            return false;
        }
        public virtual bool Run()
        {
            return false;
        }
        public virtual void TryClickInput(Point TCord) { }
        public virtual void TryClickOutput(Point TCord) { }
    }
}
